﻿using System.Net;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Channels;
using System.Web;
using System.Web.Security;
using Framework;
using ISSMoviePlex.BLL;
using ISSMoviePlex.DAL.Models;

namespace ISSMoviePlex.Service
{
    //*************************************CONCURRENCY************************************/
    //K: ConcurrencyMode.Single, need thread-safe
    //*************************************INSTANCE**************************************/
    //K: InstanceContextMode.Single because we need to save logged in user in our cookie
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single,
        ConcurrencyMode = ConcurrencyMode.Single)]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
    //IIS(8.0) URL: http://localhost/CineService/AccountService.svc
    public class AccountService : IAccountService
    {

        private readonly AccountBLL _accountLogic = new AccountBLL();

        public bool AuthorizeUser(string password, string userName, bool isRemember, AccountBLL.Role role) 
        {
            var errorMessage = string.Empty;
            var status = _accountLogic.Login(new LoginModel()
            {
                Password = password,
                RememberMe = isRemember,
                UserName = userName
            }, role, ref errorMessage);

            if (!Is.NotEqualString(errorMessage, string.Empty))
            {
                // Create Cookie
                var cookie = new HttpCookie(FormsAuthentication.FormsCookieName) { Value = userName };

                // Attach Cookie to Operation Context header
                var response = new HttpResponseMessageProperty();
                response.Headers[HttpResponseHeader.SetCookie] = cookie.Name + "=" + cookie.Value;
                OperationContext.Current.OutgoingMessageProperties
                     [HttpResponseMessageProperty.Name] = response;
                return status;
            }
            else
            {
                var mf = new AuthorizationFault
                {
                    Operation = "Login",
                    ProblemType = "User name or password is not correct!"
                };
                throw new FaultException<AuthorizationFault>(mf, new FaultReason("Something wrong occured!"));
            }
        }

        public void LogOut()
        {
            _accountLogic.Logout();
        }

        public void ChangePassword(string userName, string oldPassword, string newPassword, string confirmPassword)
        {
            var status = _accountLogic.ChangePassword(userName, new LocalPasswordModel()
            {
                OldPassword = oldPassword,
                ConfirmPassword = confirmPassword,
                NewPassword = newPassword
            });

            if (!status)
            {
                var mf = new AuthorizationFault
                {
                    Operation = "Change Password",
                    ProblemType = "Please check your input again!"
                };
                throw new FaultException<AuthorizationFault>(mf);
            }
        }

    }
}
